package com.yaodian100.ec.generate.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;

import com.yaodian100.core.admin.entity.Role;
import com.yaodian100.core.admin.entity.User;
import com.yaodian100.core.admin.service.UserService;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.mail.entity.Mail;
import com.yaodian100.core.mail.service.MailService;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.entity.Option;
import com.yaodian100.core.menu.service.MenuService;
import com.yaodian100.core.template.service.TemplateService;
import com.yaodian100.ec.generate.service.GenerateNotifyService;
import com.yaodian100.ec.product.entity.Product;
import com.yaodian100.ec.product.service.ProductService;

/**
 * @author aaron.chueh
 * 
 */
public class GenerateNotifyServiceImpl implements GenerateNotifyService {
	private static final String VM_AD_PRODUCT_SHELF_INACTIVE_NOTIFICATION="ad_product_shelf_inactive_notification";
	private static final String MENU_KEY_CATEGORY_GROUP_MAPPING="Category.Group.Mapping";
	private static final String ROLE_AD_CONTACT_WINDOW="AD_CONTACT_WINDOW";
	
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private ProductService productService;
	@Autowired
	private TemplateService templateService;
	@Autowired
	private MailService mailService;
	@Autowired
	private MenuService menuService;
	@Autowired
	private UserService userService;
	
	@Override
	public void sendNotificationForProductIdsWithProblem(Long categoryId, List<String> productIdList,
			String advType, String advName) {
		if(productIdList!=null && productIdList.size()>0){
			List errProdList=new ArrayList();
			
			//取得商品
			CommonCriteria prodCriteria=new CommonCriteria();
			prodCriteria.addIn("productId",productIdList);
			List<Product> productList=productService.getList(0, -1, prodCriteria, null);
			
			for(int i=0;i<productList.size();i++){
				Map errMap=new TreeMap();
				StringBuilder errMsg=new StringBuilder("");
				
				Product product=productList.get(i);
				
				//判斷商品目前是否上架且是否在3天以內下架
				String shelfSql="SELECT COUNT(*) FROM EC_SHELF_MAIN WHERE PRODUCT_ID = ? " +
						"AND SHELF_STATUS = 'SHF500' AND START_DATE < SYSDATE AND END_DATE > (SYSDATE+3)";
				int shelfCount=jdbcTemplate.queryForInt(shelfSql,product.getProductId());
				
				if(shelfCount==0){//0:商品目前無上架or在3天以內下架
					errMsg.append("商品目前无上架or在3天以内下架");
				}
				
				//不是N選M商品才做庫存量檢核
				if(product.getProductType()!=6){
					String inventorySql="SELECT COUNT(*) FROM EC_SPECIFICATION_INVENTORY "+
							"WHERE (AVAILABLE_INVENTORY-SOLD_INVENTORY+CANCEL_INVENTORY) > 0 AND PRODUCT_ID = ?";
					int inventoryCount=jdbcTemplate.queryForInt(inventorySql,product.getProductId());
					
					if(inventoryCount==0){//0:無庫存 1:有庫存
						if(errMsg.length()==0){
							errMsg.append("商品无库存");
						}else{
							errMsg.append(" & 商品无库存");
						}
					}
				}
				
				//檢核商品IS_DELETE狀態
				String isDeleteSql="SELECT COUNT(*) FROM EC_PRODUCT_MAIN WHERE IS_DELETE = 'N' AND PRODUCT_ID = ?";
				int isDeleteCount=jdbcTemplate.queryForInt(isDeleteSql,product.getProductId());
				
				if(isDeleteCount==0){//0:IS_DELETE!=N 1:IS_DELETE==N(可查詢可銷售)
					if(errMsg.length()==0){
						errMsg.append("商品状态不为可查询可销售");
					}else{
						errMsg.append(" & 商品状态不为可查询可销售");
					}
				}
				
				if(errMsg.length() > 0){
					errMap.put("productId", product.getProductId());
					errMap.put("errMsg", errMsg.toString());
					
					errProdList.add(errMap);
				}
			}
			
			if(errProdList.size()>0){
				//準備郵件內容
				Map<String,Object> mailContentParameters=new HashMap<String,Object>();
				mailContentParameters.put("advType",advType);
				mailContentParameters.put("advName",advName);
				mailContentParameters.put("errProdList",errProdList);
				Mail notification=templateService.formatToMail(VM_AD_PRODUCT_SHELF_INACTIVE_NOTIFICATION, mailContentParameters);
				
				//找出收件者
				Menu categoryRoleMappingMenu=menuService.get(MENU_KEY_CATEGORY_GROUP_MAPPING);
				Map<String,Option> categoryRoleMapping=categoryRoleMappingMenu.getOptions();
				String categoryIdString="0";
				if(categoryId != null){
					categoryIdString=StringUtils.trim(categoryId.toString());
				}
				String responsibleGroupCode=categoryRoleMapping.get(categoryIdString).getName();
				List<User> adContactWindowList=userService.getByRole(ROLE_AD_CONTACT_WINDOW, Role.Type.OTHER,
						responsibleGroupCode);

				for(User contactWindow:adContactWindowList){
					notification.addTo(contactWindow.getNameNative(),contactWindow.getEmail());
				}
				
				notification.addBcc("Front End","fe@yaodian100.com");
				
				mailService.save(notification);
			}
		}
	}

	//getter & setter
	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public TemplateService getTemplateService() {
		return templateService;
	}

	public void setTemplateService(TemplateService templateService) {
		this.templateService = templateService;
	}

	public MailService getMailService() {
		return mailService;
	}

	public void setMailService(MailService mailService) {
		this.mailService = mailService;
	}

	public MenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}